package easycloud.ecs.server.service;

import java.util.Date;

import javax.inject.Inject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import com.mysql.jdbc.StringUtils;

import easycloud.ecs.common.domain.User;
import easycloud.ecs.common.repository.UserRepository;
import easycloud.ecs.common.service.UserService;

@Service
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

	private static final Log logger = LogFactory.getLog(UserServiceImpl.class);

	@Inject
	private UserRepository userRepository;

	@Transactional(readOnly = false)
	public User signin(String email, String password) {
		logger.info("signin : " + email);
		User user = userRepository.getByEmail(email);
		if (user != null && !StringUtils.isNullOrEmpty(password)) {
			if (DigestUtils.md5DigestAsHex(password.getBytes()).equals(
					user.getPassword())) {
				user.setLastSigninTime(new Date());
				userRepository.save(user);
				return user;
			}
		}
		return null;
	}
}
